package com.ptswitch.material.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.security.Role;

public interface RoleMapper {
    @Select("select * from uzer_role ur,uzer u where u.username like #{username} and "//
            + " ur.userId=u.userId and ur.roleId=#{roleId}")
    List<Uzer> listUserHaveRole(String roleId, String username, long first, long size);

    @Select("select * from uzer_role ur,uzer u where u.username like #{username} and "//
            + " ur.userId=u.userId and ur.roleId=#{roleId}")
    List<Uzer> listUserNotHaveRole(String roleId, String username, long first, long size);//TODO

    @Select("select count(*) from uzer_role ur,uzer u where u.username like #{username} and "//
            + " ur.userId=u.userId and ur.roleId=#{roleId}")
    long countUserHaveRole(String roleId, String username);

    @Select("select count(*) from uzer_role ur,uzer u where u.username like #{username} and "//
            + " ur.userId=u.userId and ur.roleId=#{roleId}")
    long countUserNotHaveRole(String roleId, String username);//TODO

    //
    @Select("select * from role where id=#{id}")
    Role findById(@Param("id") String id);

//    @Select("")
//    List<Role> findRoles(Role... roles);

    @Select("select * from role where roleGroup=#{rg}")
    List<Role> findByRoleGroup(@Param("rg") String roleGroup);

    @Select("select * from role where roleGroup!='relate'")
    List<Role> findNormalRoles();

    @Select("select * from role where roleGroup='relate'")
    List<Role> findRelateRoles();

    @Insert("insert into role(displayName,inUse,roleGroup,roleName)"//
            + " values(#{role.displayName},case when #{role.inUse} then 1 else 0 end"//
            + ",#{role.roleGroup},#{role.roleName}) returning id")
    long add(@Param("role") Role role);

    @Select("select * from role")
    List<Role> findAll();

    @Select("select count(*) from role")
    long countAll();
}
